﻿using SAEA.MVC;
using KeydbCommand.Attr;
using KeydbCommand.Services;

namespace KeydbCommand.Controllers
{
    /// <summary>
    /// Keydb cluster controller
    /// </summary>
    [Auth(true)]
    public class KeydbClusterController : Controller
    {
        /// <summary>
        /// 获取cluster 节点信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult GetClusterNodes(string name)
        {
            return Json(new KeydbClusterService().GetClusterNodes(name));
        }

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ipPort"></param>
        /// <returns></returns>
        public ActionResult AddMaster(string name, string ipPort)
        {
            return Json(new KeydbClusterService().AddMaster(name, ipPort));
        }

        /// <summary>
        /// 添加从节点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="slaveNodeID"></param>
        /// <param name="masterID"></param>
        /// <returns></returns>
        public ActionResult AddSlave(string name, string slaveNodeID, string masterID)
        {
            return Json(new KeydbClusterService().AddSlave(name, slaveNodeID, masterID));
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="nodeID"></param>
        /// <returns></returns>
        public ActionResult DeleteNode(string name, string nodeID)
        {
            return Json(new KeydbClusterService().DeleteNode(name, nodeID));
        }

        /// <summary>
        /// 添加槽点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="nodeID"></param>
        /// <param name="slotStr"></param>
        /// <returns></returns>
        public ActionResult AddSlots(string name, string nodeID, string slotStr)
        {
            return Json(new KeydbClusterService().AddSlots(name, nodeID, slotStr));
        }

        /// <summary>
        /// 删除槽点
        /// </summary>
        /// <param name="name"></param>
        /// <param name="nodeID"></param>
        /// <param name="slotStr"></param>
        /// <returns></returns>
        public ActionResult DelSlots(string name, string nodeID, string slotStr)
        {
            return Json(new KeydbClusterService().DelSlots(name, nodeID, slotStr));
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="nodeID"></param>
        /// <returns></returns>
        public ActionResult SaveConfig(string name, string nodeID)
        {
            return Json(new KeydbClusterService().SaveConfig(name, nodeID));
        }
    }
}
